package ftp.action;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UploadOverwriteFile extends BaseAdapter{
	 File[] files = new File[1];
     List<File> listCheck = new ArrayList<File>();
     UploadActivity context;
     RelativeLayout flag=null;
     public static String saveFilePath;
     Bundle language;

     public Bundle getLanguage() {
             return language;
     }

     public void setLanguage(Bundle language) {
             this.language = language;
     }

     public UploadOverwriteFile() {
     }

     String duoi="";
      String ten ="";
     public UploadOverwriteFile(File[] files, UploadActivity context,Bundle language) {
             super();
             this.files = files;
             this.context = context;
             this.language=language;
     }

     @Override
     public int getCount() {
             int kq=0;
             if(files!=null){
                     kq=files.length;
             }
             return kq;
     }

     @Override
     public Object getItem(int arg0) {

             return files[arg0];
     }

     @Override
     public long getItemId(int arg0) {

             return 0;
     }

     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {

    	 LayoutInflater inflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.inflate(R.layout.uploadoverwritefile, null);
         final File file = files[position];


         final CheckBox checkBox = (CheckBox) convertView
         .findViewById(R.id.checkFile);
         final TextView txtName = (TextView) convertView
         .findViewById(R.id.txtName);
         final TextView txtSize = (TextView) convertView
         .findViewById(R.id.txtSize);
         final RelativeLayout linerFile = (RelativeLayout) convertView
         .findViewById(R.id.linerFile);

         if(file.getName().equals("..|") ){
                 txtName.setText("../");
         }else{
                 txtName.setText(file.getName());
         }

         if(file.isFile() && !(file.getName().toString().equals("..|"))){
                 checkBox.setVisibility(View.VISIBLE);
                 txtName.setTextColor(Color.parseColor("#FFFFFF"));
                 txtSize.setText(file.length()+" bytes");                        
         }else if(file.isDirectory() || (file.getName().toString().equals("..|"))){
                 if((file.getName().toString().equals("..|"))){
                         checkBox.setVisibility(View.INVISIBLE);
                 }else{
                         checkBox.setVisibility(View.VISIBLE);
                 }
                 txtName.setTextColor(Color.parseColor("#FFFF00"));
                 txtSize.setText("");
         }
         int co=0;
         for(int i=0;i<listCheck.size();i++){
                 if(file.getName().equals(listCheck.get(i).getName())){
                         checkBox.setChecked(true);
                         co=1;
                 }
         }
         if(checkBox.isChecked()){
                 if(co==0){
                         checkBox.setChecked(false);
                 }
         }
         checkBox.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View arg0) {
                         if (checkBox.isChecked()) {
                                 listCheck.add(file);
                         } else {
                                 System.out.println(file.getName());
                                 for(int i=0;i<listCheck.size();i++){
                                         if(file.getName().equals(listCheck.get(i).getName())){
                                                 listCheck.remove(i);
                                         }
                                 }
                         }

                 }
         });

         linerFile.setOnTouchListener(new View.OnTouchListener() {

                 @Override
                 public boolean onTouch(View v, MotionEvent event) {
                         if(flag!=null){
                                 flag.setBackgroundColor(Color.parseColor("#000000"));
                         }

                         if (event.getAction() == MotionEvent.ACTION_DOWN) {
                                 linerFile.setBackgroundColor(Color.parseColor("#ff6600"));
                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
                                 linerFile.setBackgroundColor(Color.parseColor("#ff6600"));
                         }
                         flag=linerFile;
                         return false;
                 }
         });

         linerFile.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View arg0) {
                         if (!file.isFile()) {
                                 context.loadLocation(file.getName());                                   

                         }
                 }
         });

         linerFile.setOnLongClickListener(new View.OnLongClickListener() {

                 @Override
                 public boolean onLongClick(View v) {
                         @SuppressWarnings("static-access")
                         LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService( context.LAYOUT_INFLATER_SERVICE);
                         View viewDelete = inflater.inflate(R.layout.deletedetail, null);

                         final TextView fname = (TextView)viewDelete.findViewById(R.id.idfilename);
                         final TextView ftype = (TextView)viewDelete.findViewById(R.id.idtypefile);
                         final TextView fsize = (TextView)viewDelete.findViewById(R.id.idsize);
                         final TextView fmodify = (TextView)viewDelete.findViewById(R.id.idmodifydate);

                         fname.setText(language.getString("filename")+":");
                         ftype.setText(language.getString("filetype")+":");
                         fsize.setText(language.getString("filesize")+":");
                         fmodify.setText(language.getString("lastmodify")+":");

                         final TextView vfname = (TextView)viewDelete.findViewById(R.id.valuenamefile);
                         final TextView vftype = (TextView)viewDelete.findViewById(R.id.valuetypefile);
                         final TextView vfSize = (TextView)viewDelete.findViewById(R.id.valuesize);
                         final TextView vfModify = (TextView)viewDelete.findViewById(R.id.valuemodifydate);

                         vfname.setText(file.getName().toString());
                         if(file.isDirectory()){
                                 vftype.setText(language.getString("folder"));
                         }else if(file.isFile()){
                                 vftype.setText(language.getString("file"));
                         }

                         if(file.isFile()){
                                 vfSize.setText(file.length() + " bytes") ;
                         }else{
                                 vfSize.setText(getTotalSize(file) + " bytes");
                         }


                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                         for(File file : files){
                                 file = files[position];
                                 Date date = new Date(file.lastModified());
                                 String s = sdf.format(date);
                                 vfModify.setText(s);
                         }

                         AlertDialog.Builder builderDetail = new AlertDialog.Builder(context);
                         builderDetail.setTitle(file.getName().toString()+" "+language.getString("properties"));
                         builderDetail.setView(viewDelete);

                         builderDetail.setNegativeButton(language.getString("cancel"), new DialogInterface.OnClickListener() {

                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {

                                 }
                         });
                         AlertDialog al = builderDetail.create();
                         al.show();
						return false;    
                 }
         });
             
             return convertView;
     }

     public List<File> getListCheck() {
             return listCheck;
     }

     public void setListCheck(List<File> listCheck) {
             this.listCheck = listCheck;
     }

     public long getTotalSize(File file) {
             //              StatFs stat = new StatFs(file.getPath());
             //              long blockSize = stat.getBlockSize();
             //              long totalBlocks = stat.getBlockCount();
             //              return totalBlocks * blockSize;

             long result = 0;
             File[] fileList = file.listFiles();

             for(int i = 0; i < fileList.length; i++) {
                     if(fileList[i].isDirectory()) {
                             result += getTotalSize(fileList [i]);
                     } else {
                             result += fileList[i].length();
                     }
             }
             return result; 
     }


     public void deleteRecursive(File dir)
     {
             if (dir.isDirectory())
             {
                     String[] children = dir.list();
                     for (int i = 0; i < children.length; i++) 
                     {
                             File temp =  new File(dir, children[i]);
                             if(temp.isDirectory())
                             {
                                     deleteRecursive(temp);
                             }
                             else
                             {
                                     boolean b = temp.delete();
                                     if(b == false)
                                     {
                                             Log.d("DeleteRecursive", "DELETE FAIL");
                                     }
                             }
                     }

                     dir.delete();
             }
             else if(dir.isFile()){
                     dir.delete();
             }
     }

     public String getSaveFilePath() {
             return saveFilePath;
     }

     public void setSaveFilePath(String saveFilePath) {
             this.saveFilePath = saveFilePath;
     }

}
